﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;

namespace ud_gSDMS.project
{
    public partial class manage : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        protected string _pageId;

        void Page_Load(object sender, EventArgs e)
        {
           
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                _pageId = Request["CurrentPage.PageId"];
                //_pageId = pageId.ToString();
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                } 
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datTimeProject_Date_from.SelectedDate = DateTime.Now.AddMonths(-3);
            datTimeProject_Date_to.SelectedDate = DateTime.Now;
            datTimeProject_Date_from.MaxDate = DateTime.Now;
            datTimeProject_Date_to.MaxDate = DateTime.Now;
            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            // //cboAccount.Text.Trim() == "" ? null : cboAccount.SelectedValue,
            var data = gSDMS_BL.ud_gSDMS_PROJECT_Search(datTimeProject_Date_from.SelectedDate == null ? null : datTimeProject_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datTimeProject_Date_to.SelectedDate == null ? null : datTimeProject_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         string.IsNullOrEmpty(cbProjectCode.SelectedValue) ? null : cbProjectCode.SelectedValue,
                                                         cboWorkStatus.Text.Trim() == "" ? null : cboWorkStatus.SelectedValue);

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }
            grdProjectList.DataSource = data;
            grdProjectList.DataBind();
        }
        private void LoadControl()
        {
            var db = new gSDMS_DataLinq();
            cboWorkStatus.DataValueField = "CAT_Project_Step_ID";
            cboWorkStatus.DataTextField = "Name";
            cboWorkStatus.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_PROJECT_STEPs);
            //cboStatus.DataSource = gSDMS_BL.ud_gSDMS_TIMESHEET_Search(null, null, null, null);
            cboWorkStatus.DataBind();

            DataTable tb = Library.LINQToDataTable(db, db.ud_gSDMS_PROJECTs);
            cbProjectCode.DataSource = tb;
            cbProjectCode.DataValueField = "Project_ID";
            cbProjectCode.DataTextField = "Code";
            cbProjectCode.DataBind();
        }

        private void grdProjectList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdProjectList.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
       
        void grdProjectList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            grdProjectList.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;

            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }
        private void grdProjectList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "editProject":
                    WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/project/project_detail.aspx?pageid=8&id=" + id);
                    break;
            }
        }
        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdProjectList.PageIndexChanging += grdProjectList_PageIndexChanging;
            grdProjectList.Sorting += grdProjectList_Sorting;
            grdProjectList.RowCommand += grdProjectList_RowCommand;
            btnSearch.Click += btnSearch_Click;
            SuppressPageMenu();
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}